--
-- Author:      feilong
-- DateTime:    2019-11-22 15:57:48
-- Description: 百人游戏所特有的一些公用功能


local error_code = require("error_code")
local constant = require("constant")
-- local UserInfo = import("UserInfo")

local math_random = math.random
local math_abs = math.abs
local math_floor = math.floor
local table_copy = table.copy
local table_insert = table.insert 
local table_remove = table.remove 
local string_split = string.split
local table_sort = table.sort




local BaiRenDesk = class("BaiRenDesk")

function BaiRenDesk:ctor(message_dispatch)
	self.message_dispatch = message_dispatch

end

function BaiRenDesk:start()
	self.user_manager = global.user_manager
	self.room_config = global.room_config
	self.rule = global.rule
	self.desk = global.desk

	self.room_data = self.room_config:getRoomData()


end


--找出富豪与神算子
function BaiRenDesk:getSpecialRobot()   

    local user_list = self.user_manager:getUserList()    
    local users = {}
    local banker = self.desk.banker
    for k, user in pairs(user_list) do 
        if not user:compare(banker) and not user:getDisconnect() then 
            table_insert(users, user)
        end
        user.win_rate = user:getLatelyWinRate() --最近20局赢率
    end
    dbglog("__________users__", self.user_manager:getUserNum())
    --富豪No.1
    table_sort(users, function(a, b)
        if a.total_bet ~= b.total_bet then
            return a.total_bet > b.total_bet
        else
            return a.win_count > b.win_count --赢的局数
        end
    end)    
    local rich_user_id = nil
    if users[1] then 
        rich_user_id = users[1]:getUid()
        table_remove(users, 1)
    end
    dbglog("__________users__2___", rich_user_id) 
    --运气王(获胜局数/玩牌局数 → 下注金 → 获胜局数)
    table_sort(users, function(a,b)
        if a.win_rate ~= b.win_rate then
            return a.win_rate > b.win_rate
        elseif a.total_bet ~= b.total_bet then
            return a.total_bet > b.total_bet
        else
            return a.win_count > b.win_count 
        end
    end)    
    local luck_user_id = nil
    if users[1] then 
        luck_user_id = users[1]:getUid()
    end
    dbglog("__________users__3___", luck_user_id)

    return rich_user_id, luck_user_id
end

--富豪与神算子坐下
--真人也可以坐下的
function BaiRenDesk:specialRobotSitdown()  
    local rich_user_id, luck_user_id = self:getSpecialRobot()
   
    if not rich_user_id or not luck_user_id then 
        dbglog("___找不到富豪或神算子__rich_user_id, luck_user_id______", rich_user_id, luck_user_id)
        return
    end

    local old_rich_user = self.user_manager:getSeatUser(1)
    local old_luck_user = self.user_manager:getSeatUser(4)
    local rich_user = self.user_manager:getUser(rich_user_id)
    local luck_user = self.user_manager:getUser(luck_user_id)

    if not rich_user:compare(old_rich_user) then
        if old_rich_user then 
            local uid = old_rich_user:getUid()
            local res = self.user_manager:standUp(uid)
            if res then 
                self.desk:sendToAllUser('table.NTF_STANDUP', {uid=uid, seat_id=1})
            end
        end

   end

   if not luck_user:compare(old_luck_user) then
        if old_luck_user then 
            local uid = old_luck_user:getUid()
            local res = self.user_manager:standUp(uid)
            if res then 
                self.desk:sendToAllUser('table.NTF_STANDUP', {uid=uid, seat_id=4})
            end
        end
        local seat_id = luck_user:getSeatId()
        if seat_id > 0 and seat_id ~= 4 then 
            local uid = luck_user:getUid()
            self.user_manager:standUp(uid)
            self.desk:sendToAllUser('table.NTF_STANDUP', {uid=uid, seat_id=seat_id})

        end
        if seat_id ~= 4 then 
            self.user_manager:sitDown(luck_user_id, 4)
            local info = luck_user:getClientInfo()
            self.desk:sendToAllUser('table.NTF_SITDOWN',{player = info})
        end
    end

    local seat_id = rich_user:getSeatId()
    if seat_id > 0 and seat_id ~= 1 then 
        local uid = rich_user:getUid()
        self.user_manager:standUp(uid)
        self.desk:sendToAllUser('table.NTF_STANDUP', {uid=uid, seat_id=seat_id})
    end
    if seat_id ~= 1 then 
        self.user_manager:sitDown(rich_user_id, 1)
        local info = rich_user:getClientInfo()
        self.desk:sendToAllUser('table.NTF_SITDOWN',{player = info})
    end

end



















return BaiRenDesk